<template>
	<view>
		<view class="order-submission">
			<view class="allAddress">
				<view class="nav acea-row">
					<view class="item font-color" :class="shippingType == 0 ? 'on' : 'on2'" @tap="addressType(0)">
					</view>
					<view class="item font-color" :class="shippingType == 1 ? 'on' : 'on2'" @tap="addressType(1)">
					</view>
				</view>
				<view class="address acea-row row-between-wrapper" @tap="showStoreList">
					<block v-if="storeList.length > 0">
						<view class="title">付款门店</view>
						<view class="addressCon">
							<view class="name">
								{{ system_store.name }}
								<text class="phone">{{ system_store.phone }}</text>
							</view>
							<view class="line1">{{ system_store.address }}</view>
						</view>
						<view class="addressIconfont iconfont icon-jiantou"></view>
					</block>
					<block v-else>
						<view>暂无门店信息</view>
					</block>
				</view>
				<view class="address acea-row row-between-wrapper" @tap="onAddress" v-if="shippingType == 0">
					<view class="title">收货地址</view>
					<view class="addressCon" v-if="addressInfo.realName">
						<view class="name">
							{{ addressInfo.realName }}
							<text class="phone">{{ addressInfo.phone }}</text>
						</view>
						<view>
							<text class="default font-color" v-if="addressInfo.isDefault">[默认]</text>
							{{ addressInfo.province }}{{ addressInfo.city }}{{ addressInfo.district }}{{ addressInfo.detail }}
						</view>
					</view>
					<view class="addressCon" v-else>
						<view class="setaddress">设置收货地址</view>
					</view>
					<view class="addressIconfont iconfont icon-jiantou"></view>
				</view>
				<view class="address acea-row row-between-wrapper" v-else>
					<view>
						<view class="contact acea-row row-between-wrapper">
							<view class="title">联系人</view>
							<view class="input"><input type="text" placeholder="请填写您的联系姓名"
									placeholder-class="placeholder" v-model="realName" /></view>
						</view>
						<view class="contact acea-row row-between-wrapper">
							<view class="title">联系电话</view>
							<view class="input"><input type="text" placeholder="请填写您的联系电话"
									placeholder-class="placeholder" v-model="phone" /></view>
						</view>
					</view>
				</view>
				<view class="line">
					<image src="/static/images/line.jpg"></image>
				</view>
			</view>
			<orderGoods :cartInfo="cartInfo"></orderGoods>
			<view class="moneyList">
				<view class="item acea-row row-between-wrapper" v-if="priceGroup.userMarkPrice > 0">
					<view>优惠：</view>
					<view class="money">-￥{{ priceGroup.userMarkPrice }}</view>
				</view>
				<view class="item acea-row row-between-wrapper">
					<view>商品总价：</view>
					<view class="money">￥{{ priceGroup.totalPrice }}</view>
				</view>
			</view>
			<view class="wrapper">
				<view class="item" v-if="textareaStatus">
					<view>备注信息</view>
					<textarea placeholder-class="placeholder" v-model="mark" name="mark"
						placeholder="请添加备注（150字以内）"></textarea>
				</view>
			</view>
			<view style="height:120rpx;"></view>
			<view class="footer acea-row row-between-wrapper">
				<view>
					合计:
					<text class="font-color">￥{{ totalPrice || 0 }}</text>
				</view>
				<view class="settlement" style="z-index:100" @tap="SubOrder">提交订单</view>
			</view>
		</view>
		<addressWindow ref="addressWindow" @changeTextareaStatus="changeTextareaStatus" :address="address"
			:pagesUrl="pagesUrl" @OnChangeAddress="OnChangeAddress" @changeClose="changeClose"></addressWindow>
		<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize>
	</view>
</template>
<script>
	import {
		orderConfirm,
		getCouponsOrderPrice,
		orderCreate,
		postOrderComputed,
		orderPay,
		wechatOrderPay,
		wechatQueryPayResult
	} from '@/api/order.js';
	import {
		getAddressDefault,
		getAddressDetail
	} from '@/api/user.js';
	import {
		openPaySubscribe
	} from '@/utils/SubscribeMessage.js';
	import {
		storeListApi
	} from '@/api/store.js';
	import {
		CACHE_LONGITUDE,
		CACHE_LATITUDE
	} from '@/config/cache.js';
	import addressWindow from '@/components/addressWindow';
	import orderGoods from '@/components/orderGoods';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from 'vuex';
	import authorize from '@/components/Authorize';

	export default {
		components: {
			addressWindow,
			orderGoods,
			authorize
		},
		data() {
			return {
				textareaStatus: true,
				cartId: '', //购物车id
				storeList: [],
				system_store: {},
				address: {
					address: false
				}, //地址组件
				addressInfo: {}, //地址信息
				addressId: 0, //地址id
				shippingType: 0,
				realName: '',
				phone: '',
				mark: '', //备注信息
				toPay: false, //修复进入支付时页面隐藏从新刷新页面
				userInfo: {}, //用户信息
				cartInfo: [],
				orderKey: '',
				priceGroup: {},
				totalPrice: 0,
				pagesUrl: '',
				payType: 'offline', //支付方式
				payChannel: 'routine',
				offlinePostage: '',
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				news: true,
				again: false,
				addAgain: false
			};
		},
		computed: mapGetters(['isLogin']),
		onLoad: function(options) {
			if (!options.cartId)
				return this.$util.Tips({
					title: '请选择要购买的商品'
				}, {
					tab: 3,
					url: 1
				});
			this.couponId = options.couponId || 0;
			this.pinkId = options.pinkId ? parseInt(options.pinkId) : 0;
			this.addressId = options.addressId || 0;
			this.cartId = options.cartId;
			this.news = options.new || true;
			this.again = options.again || false;
			this.addAgain = options.addAgain || false;
			this.secKill = options.secKill || false;
			this.combination = options.combination || false;
			this.bargain = options.bargain || false;
			if (this.isLogin) {
				this.getConfirm();
				this.getaddressInfo();
				this.getList();
			} else {
				this.isAuto = true;
				this.$set(this, 'isShowAuth', true);
			}
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			let that = this;
			this.textareaStatus = true;
			if (this.isLogin && this.toPay == false) {
				this.$nextTick(function() {
					this.$refs.addressWindow.getAddressList();
				});
			}

			uni.$on('handClick', res => {
				if (res) {
					that.system_store = res.address;
				}
				// 清除监听
				uni.$off('handClick');
			});
		},
		methods: {
			/**
			 * 授权回调事件
			 *
			 */
			onLoadFun: function() {
				// this.getaddressInfo();
				// this.getConfirm();
				//调用子页面方法授权后执行获取地址列表
				// this.$scope.selectComponent('#address-window').getAddressList();
			},
			addressType: function(e) {
				this.shippingType = parseInt(e);
			},
			/**
			 * 获取门店列表数据
			 */
			getList: function() {
				let longitude = uni.getStorageSync('user_longitude'); //经度
				let latitude = uni.getStorageSync('user_latitude'); //纬度
				let data = {
					latitude: latitude, //纬度
					longitude: longitude, //经度
					page: 1,
					limit: 10
				};
				storeListApi(data)
					.then(res => {
						let list = res.data || [];
						this.$set(this, 'storeList', list);
						this.$set(this, 'system_store', list[0]);
					})
					.catch(err => {
						return this.$util.Tips({
							title: err
						});
					});
			},
			/*
			 * 跳转门店列表
			 */
			showStoreList: function() {
				let _this = this;
				if (this.storeList.length > 0) {
					uni.navigateTo({
						url: '/pages/store_list/index'
					});
				}
			},
			computedPrice: function() {
				let shippingType = this.shippingType;
				postOrderComputed(this.orderKey, {
						addressId: this.addressId,
						useIntegral: this.useIntegral ? 1 : 0,
						couponId: this.couponId,
						shippingType: parseInt(shippingType) + 1,
						payType: this.payType
					})
					.then(res => {
						let result = res.data.result.result;
						if (result) {
							this.totalPrice = result.orderId.payPrice;
							this.integral_price = result.orderId.deductionPrice;
							this.coupon_price = result.orderId.couponPrice;
							this.integral = this.useIntegral ? result.orderId.surplusIntegral : this.userInfo
								.integral;
							this.$set(this.priceGroup, 'storePostage', shippingType == 1 ? 0 : result.orderId
								.payPostage);
						}
					})
			},
			onAddress: function() {
				let that = this;
				that.textareaStatus = false;
				that.address.address = true;
				that.pagesUrl =
					'/pages/users/user_address_list/index?cartId=' +
					this.cartId +
					'&pinkId=' +
					this.pinkId +
					'&couponId=' +
					this.couponId +
					'&secKill=' +
					this.secKill +
					'&combination=' +
					this.combination +
					'&bargain=' +
					this.bargain;
			},
			// 关闭地址弹窗；
			changeClose: function() {
				this.$set(this.address, 'address', false);
			},
			changeTextareaStatus: function() {
				// for (let i = 0, len = this.coupon.list.length; i < len; i++) {
				// 	this.coupon.list[i].use_title = '';
				// 	this.coupon.list[i].is_use = 0;
				// }
				// this.textareaStatus = true;
				// this.status = 0;
				// this.$set(this.coupon, 'list', this.coupon.list);
			},
			/**
			 * 选择地址后改变事件
			 * @param object e
			 */
			OnChangeAddress: function(e) {
				this.textareaStatus = true;
				this.addressId = e;
				this.address.address = false;
				this.getaddressInfo();
			},
			/**
			 * 获取当前订单详细信息
			 *
			 */
			getConfirm: function() {
				let that = this;
				orderConfirm(that.cartId, that.news, this.addAgain, this.secKill, this.combination, this.bargain)
					.then(res => {
						that.$set(that, 'userInfo', res.data.userInfo);
						that.$set(that, 'realName', res.data.userInfo.realName);
						that.$set(that, 'phone', res.data.userInfo.phone);
						that.$set(that, 'cartInfo', res.data.cartInfo);
						that.$set(that, 'orderKey', res.data.orderKey);
						that.$set(that, 'priceGroup', res.data.priceGroup);
						that.$set(that, 'totalPrice', that.$util.$h.Add(parseFloat(res.data.priceGroup.totalPrice),
							parseFloat(res.data.priceGroup.storePostage)));
					})
					.catch(err => {
						return this.$util.Tips({
							title: err
						}, {
							tab: 3,
							url: 1
						});
					});
			},
			/*
			 * 获取默认收货地址或者获取某条地址信息
			 */
			getaddressInfo: function() {
				let that = this;
				if (that.addressId) {
					getAddressDetail(that.addressId).then(res => {
						if (res.data) {
							res.data.isDefault = parseInt(res.data.isDefault);
							that.addressInfo = res.data || {};
							that.addressId = res.data.id || 0;
							that.address.addressId = res.data.id || 0;
						}
					});
				} else {
					getAddressDefault().then(res => {
						if (res.data) {
							res.data.isDefault = parseInt(res.data.isDefault);
							that.addressInfo = res.data || {};
							that.addressId = res.data.id || 0;
							that.address.addressId = res.data.id || 0;
						}
					});
				}
			},
			payment: function(data) {
				let that = this;
				let pid = getApp().globalData.spid;
				pid = parseInt(pid);
				if (pid) {
					data.spreadPid = pid
				}
				orderCreate(that.orderKey, data)
					.then(res => {
						return that.$util.Tips({
							title: '提交成功',
							icon: 'success'
						}, {
							tab: 4,
							url: '/pages/order_pay_status/index?order_id=' + res.data.orderNo + '&msg=支付成功'
						});
					})
					.catch(err => {
						uni.hideLoading();
						return that.$util.Tips({
							title: err
						});
					});
			},
			SubOrder: function(e) {
				let that = this,
					data = {};
				if (!that.system_store.id) {
					return that.$util.Tips({
						title: '暂无门店,请选择其他方式'
					});
				}
				if (that.shippingType == 0 && !that.addressId) {
					return that.$util.Tips({
						title: '请选择收货地址'
					});
				}
				if (that.shippingType == 1) {
					if (that.realName == '' || that.phone == '') {
						return that.$util.Tips({
							title: '请填写联系人或联系人电话'
						});
					}
					if (!/^[\u4e00-\u9fa5\w]{2,16}$/.test(that.realName)) {
						return that.$util.Tips({
							title: '请填写您的真实姓名'
						});
					}
					if (!/^1(3|4|5|7|8|9|6)\d{9}$/.test(that.phone)) {
						return that.$util.Tips({
							title: '请填写正确的手机号'
						});
					}
				}
				data = {
					storeId: that.system_store.id || 0,
					shopId: that.system_store.id || 0,
					addressId: that.addressId,
					realName: that.realName,
					phone: that.phone,
					mark: that.mark,
					formId: '',
					couponId: that.couponId || 0,
					payType: that.payType,
					useIntegral: that.useIntegral || 'false',
					bargainId: that.BargainId || 0,
					combinationId: that.combinationId || 0,
					pinkId: that.pinkId || 0,
					seckillId: that.seckillId || 0,
					shippingType: that.$util.$h.Add(that.shippingType, 1),
					isNew: that.news,
					payChannel: that.payChannel
				};
				uni.showLoading({
					title: '订单提交中'
				});
				that.payment(data);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.textR {
		text-align: right;
	}

	.order-submission .line {
		width: 100%;
		height: 3rpx;
	}

	.order-submission .line image {
		width: 100%;
		height: 100%;
		display: block;
	}

	.order-submission .address {
		padding: 28rpx 30rpx;
		background-color: #fff;
		box-sizing: border-box;
	}

	.order-submission .address .title {
		width: 130rpx;
		font-size: 30rpx;
		color: #282828;
	}

	.order-submission .address .addressCon {
		width: 475rpx;
		font-size: 26rpx;
		color: #666;
	}

	.order-submission .address .addressCon .name {
		font-size: 30rpx;
		color: #282828;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.order-submission .address .addressCon .name .phone {
		margin-left: 50rpx;
	}

	.order-submission .address .addressCon .default {
		margin-right: 12rpx;
	}

	.order-submission .address .addressCon .setaddress {
		color: #333;
		font-size: 28rpx;
	}

	.order-submission .address .contact {
		padding: 14rpx 0;
		font-size: 30rpx;
		color: #282828;
	}

	.order-submission .address .addressIconfont {
		width: 40rpx;
	}

	.order-submission .address .iconfont {
		font-size: 35rpx;
		color: #707070;
	}

	.order-submission .allAddress {
		width: 100%;
		background: linear-gradient(to bottom, #e93323 0%, #f5f5f5 100%);
		// background-image: linear-gradient(to bottom, #e93323 0%, #f5f5f5 100%);
		// background-image: -webkit-linear-gradient(to bottom, #e93323 0%, #f5f5f5 100%);
		// background-image: -moz-linear-gradient(to bottom, #e93323 0%, #f5f5f5 100%);
		padding-top: 100rpx;
	}

	.order-submission .allAddress .nav {
		width: 710rpx;
		margin: 0 auto;
	}

	.order-submission .allAddress .nav .item {
		width: 355rpx;
	}

	.order-submission .allAddress .nav .item.on {
		position: relative;
		width: 250rpx;
	}

	.order-submission .allAddress .nav .item.on::before {
		position: absolute;
		bottom: 0;
		content: '快递配送';
		font-size: 28rpx;
		display: block;
		height: 0;
		width: 336rpx;
		border-width: 0 20rpx 80rpx 0;
		border-style: none solid solid;
		border-color: transparent transparent #fff;
		z-index: 2;
		border-radius: 7rpx 30rpx 0 0;
		text-align: center;
		line-height: 80rpx;
	}

	.order-submission .allAddress .nav .item:nth-of-type(2).on::before {
		content: '到店自提';
		border-width: 0 0 80rpx 20rpx;
		border-radius: 30rpx 7rpx 0 0;
	}

	.order-submission .allAddress .nav .item.on2 {
		position: relative;
	}

	.order-submission .allAddress .nav .item.on2::before {
		position: absolute;
		bottom: 0;
		content: '到店自提';
		font-size: 28rpx;
		display: block;
		height: 0;
		width: 400rpx;
		border-width: 0 0 60rpx 60rpx;
		border-style: none solid solid;
		border-color: transparent transparent #f7c1bd;
		border-radius: 40rpx 6rpx 0 0;
		text-align: center;
		line-height: 60rpx;
	}

	.order-submission .allAddress .nav .item:nth-of-type(1).on2::before {
		content: '快递配送';
		border-width: 0 60rpx 60rpx 0;
		border-radius: 6rpx 40rpx 0 0;
	}

	.order-submission .allAddress .address {
		width: 710rpx;
		margin: 0 auto;
	}

	.order-submission .allAddress .line {
		width: 710rpx;
		margin: 0 auto;
	}

	.order-submission .wrapper .item .discount .placeholder {
		color: #ccc;
	}

	.order-submission .wrapper {
		background-color: #fff;
		margin-top: 13rpx;
	}

	.order-submission .wrapper .item {
		padding: 27rpx 30rpx;
		font-size: 30rpx;
		color: #282828;
		border-bottom: 1px solid #f0f0f0;
	}

	.order-submission .wrapper .item .discount {
		font-size: 30rpx;
		color: #999;
	}

	.order-submission .wrapper .item .discount .iconfont {
		color: #515151;
		font-size: 30rpx;
		margin-left: 15rpx;
	}

	.order-submission .wrapper .item .discount .num {
		font-size: 32rpx;
		margin-right: 20rpx;
	}

	.order-submission .wrapper .item .shipping {
		font-size: 30rpx;
		color: #999;
		position: relative;
		padding-right: 58rpx;
	}

	.order-submission .wrapper .item .shipping .iconfont {
		font-size: 35rpx;
		color: #707070;
		position: absolute;
		right: 0;
		top: 50%;
		transform: translateY(-50%);
		margin-left: 30rpx;
	}

	.order-submission .wrapper .item textarea {
		background-color: #f9f9f9;
		width: 690rpx;
		height: 140rpx;
		border-radius: 3rpx;
		margin-top: 30rpx;
		padding: 25rpx 28rpx;
		box-sizing: border-box;
	}

	.order-submission .wrapper .item .placeholder {
		color: #ccc;
	}

	.order-submission .wrapper .item .list {
		margin-top: 35rpx;
	}

	.order-submission .moneyList {
		margin-top: 12rpx;
		background-color: #fff;
		padding: 30rpx;
	}

	.order-submission .moneyList .item {
		font-size: 28rpx;
		color: #282828;
	}

	.order-submission .moneyList .item~.item {
		margin-top: 20rpx;
	}

	.order-submission .moneyList .item .money {
		color: #868686;
	}

	.order-submission .footer {
		width: 100%;
		height: 100rpx;
		background-color: #fff;
		padding: 0 30rpx;
		font-size: 28rpx;
		color: #333;
		box-sizing: border-box;
		position: fixed;
		bottom: 0;
		left: 0;
	}

	.order-submission .footer .settlement {
		font-size: 30rpx;
		color: #fff;
		width: 240rpx;
		height: 70rpx;
		background-color: #e93323;
		border-radius: 50rpx;
		text-align: center;
		line-height: 70rpx;
	}

	.footer .transparent {
		opacity: 0;
	}
</style>
